//
//  ViewController.m
//  音乐播放器
//
//  Created by dllo on 16/3/19.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "ViewController.h"
#import "PlayMusic.h"

@interface ViewController ()<PlayMusicDelegate>

@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, assign) BOOL isPause;

@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) UISlider *slider1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [PlayMusic shareMusic].delegate = self;
    
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(100, 100, 100, 50);
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [button setTitle:@"播放" forState:(UIControlStateNormal)];

    
    UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button2.frame = CGRectMake(100, 200, 100, 50);
    button2.backgroundColor = [UIColor redColor];
    [self.view addSubview:button2];
    [button2 addTarget:self action:@selector(buttonAction2:) forControlEvents:(UIControlEventTouchUpInside)];
    [button2 setTitle:@"暂停" forState:(UIControlStateNormal)];
    
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 300, 375 - 20, 50)];
    [self.view addSubview:_slider];
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:(UIControlEventValueChanged)];
    _slider.maximumValue = 216;
    
    
    self.slider1 = [[UISlider alloc] initWithFrame:CGRectMake(10, 380, 375 - 20, 50)];
    [self.view addSubview:_slider1];
    [_slider1 addTarget:self action:@selector(slider1Action:) forControlEvents:(UIControlEventValueChanged)];
    _slider1.maximumValue = 10;
    _slider1.value = [[PlayMusic shareMusic] createPlayVolume];
    
}

- (void)slider1Action:(UISlider *)slider {
    
    [PlayMusic shareMusic].volume = slider.value;
}

- (void)sliderAction:(UISlider *)slider {
    
    [[PlayMusic shareMusic] seekToTime:slider.value];
}

- (void)playTime:(CGFloat)time {
    
    _slider.value = time;
}



- (void)buttonAction2:(UIButton *)button {
    if (!_isPause) {
        [button setTitle:@"播放" forState:(UIControlStateNormal)];
        [[PlayMusic shareMusic] pausePlay];
    } else {
        [button setTitle:@"暂停" forState:(UIControlStateNormal)];
        [[PlayMusic shareMusic] startPlay];
    }
    _isPause = !_isPause;
}

- (void)buttonAction:(UIButton *)button {
    
    if (_isFirst) {
        [[PlayMusic shareMusic] playMusicWithAvUrl:@"http://om32.alicdn.com/859/859/4191/51570_38221_l.m4a?auth_key=25c31aa5f54e90cfa8889c50f45141cd-1458432000-0-null" musicDuration:216];
    } else {
        [[PlayMusic shareMusic] playMusicWithAvUrl:@"http://m5.file.xiami.com/198/1198/6092/74560_10709259_l.mp3?auth_key=7b00a0102053483e54230744168d61d6-1458432000-0-null" musicDuration:216];
    }
    
    [[PlayMusic shareMusic] startPlay];
   
    _isFirst = !_isFirst;
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
